package model

import (
	"background/db"
	"background/logs"
	"fmt"
	"log"
)

type DocTemplate struct {
	Id int32 `sql:"id" json:"id"`
	Content string `sql:"content" json:"content"`
	Name string `sql:"name" json:"name"`
}


func CreateDocTemplate(templ DocTemplate)  error{
	sql := fmt.Sprintf(`insert into doc_template(content,name) values('%s','%s')`,templ.Content,templ.Name)
	log.Print(sql)
	_, e := db.GetMysqlClient().Query(sql)
	if nil != e {
		log.Print(sql)
		return e
	}
	return nil
}

func UpdateDocTemplate(templ DocTemplate)  error{
	sql := fmt.Sprintf(`update  doc_template set  content = '%s',name = '%s'  where  doc_template.id = %d`,templ.Content,templ.Name,templ.Id)
	log.Print(sql)
	_, e := db.GetMysqlClient().Query(sql)
	if nil != e {
		log.Print(e.Error())
		return e
	}
	return nil
}

func DeleteDocTemplate(id int32)  error{
	sql := fmt.Sprintf(`delete from doc_template where id = '%d'`,id)
	_, e := db.GetMysqlClient().Query(sql)
	if nil != e {
		logs.Error(e.Error())
		return e
	}
	return nil
}

func ReadDocTemplate(id int32)  ([]DocTemplate,error){
	memo := []DocTemplate{}
	sql := fmt.Sprintf(`select * from doc_template where doc_template.id = '%d'`,id)
	e := db.GetMysqlClient().Query2(sql,&memo)
	if nil != e {
		logs.Error(e.Error())
		return nil,e
	}
	return memo,nil
}

func GetDocTemplate(title string,limit int,offset int) ([]DocTemplate,error) {
	memo :=  []DocTemplate{}
	sql := fmt.Sprintf(`select * from doc_template  limit %d offset %d`,
		limit,offset)
	log.Print(sql)
	e := db.GetMysqlClient().Query2(sql,&memo)
	if nil != e {
		logs.Error(e.Error())
		return nil,e
	}
	return memo,nil
}